package com.lovelyj.trip_by_train.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lovelyj.trip_by_train.common.R;
import com.lovelyj.trip_by_train.dto.MyOrderDto;
import com.lovelyj.trip_by_train.dto.TrainOrderRequestDto;
import com.lovelyj.trip_by_train.entity.DailyRemainTicket;
import com.lovelyj.trip_by_train.entity.Order;
import com.lovelyj.trip_by_train.entity.Passenger;
import com.lovelyj.trip_by_train.entity.Ticket;
import com.lovelyj.trip_by_train.service.DailyRemainTicketService;
import com.lovelyj.trip_by_train.service.OrderService;
import com.lovelyj.trip_by_train.service.TicketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private DailyRemainTicketService dailyRemainTicketService;

    @Autowired
    private TicketService ticketService;


    @PutMapping("/create")
    public R<String> create(HttpServletRequest request, @RequestBody TrainOrderRequestDto trainOrderRequestDto) {

        log.info(trainOrderRequestDto.toString());

        Long userId=request.getSession().getAttribute("user")==null?null:(Long)request.getSession().getAttribute("user");

        Long bookTicketId=trainOrderRequestDto.getBookTicketId();

        DailyRemainTicket dailyRemainTicket=dailyRemainTicketService.getById(bookTicketId);

        Integer seatType=trainOrderRequestDto.getSeatType();

        LocalDateTime time=LocalDateTime.now();


        log.info(time.toString());

        Order order=new Order();

        order.setUserId(userId);

        order.setOrderTime(time);

        order.setTotalPrice(trainOrderRequestDto.getTotalPrice());

        orderService.save(order);

        log.info(order.toString());

        for(Passenger passenger:trainOrderRequestDto.getPassengers()){
            Ticket ticket=new Ticket();
            ticket.setOrderId(order.getId());
            ticket.setTicketType(passenger.getType());
            ticket.setStartStation(dailyRemainTicket.getStartStation());
            ticket.setEndStation(dailyRemainTicket.getEndStation());
            ticket.setSeatType(seatType);
            ticket.setStartTime(dailyRemainTicket.getStartTime());
            ticket.setEndTime(dailyRemainTicket.getEndTime());
            ticket.setPassengerName(passenger.getName());
            ticket.setPassengerIdType(passenger.getIdType());
            ticket.setPassengerId(passenger.getIdCard());
            ticket.setTrainNum(dailyRemainTicket.getTrainNum());
            ticketService.createTicket(ticket,bookTicketId);
        }

        return R.success("success");
    }

    @GetMapping
    public R<List<MyOrderDto>> getMyOrder(HttpServletRequest request){
        Long userId=request.getSession().getAttribute("user")==null?null:(Long)request.getSession().getAttribute("user");

        LambdaQueryWrapper<Order> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Order::getUserId,userId);
        List<Order> orders=orderService.list(queryWrapper);
        List<MyOrderDto> myOrderDtos=new ArrayList<>();
        for(Order order:orders){
            MyOrderDto myOrderDto=new MyOrderDto();
            Long orderId=order.getId();
            LambdaQueryWrapper<Ticket> ticketQueryWrapper=new LambdaQueryWrapper<>();
            ticketQueryWrapper.eq(Ticket::getOrderId,orderId);
            List<Ticket> tickets=ticketService.list(ticketQueryWrapper);
            myOrderDto.setOrderTime(order.getOrderTime());
            myOrderDto.setOrderId(order.getId());
            myOrderDto.setTickets(tickets);
            myOrderDtos.add(myOrderDto);
        }
        return R.success(myOrderDtos);


    }



}
